package com.yycx.module.bus.provider.handler.process;

import com.yycx.bpm.provider.common.handler.ProcessStartHandler;
import com.yycx.bpm.provider.model.ProcessInstanceRequest;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 启动资金支付审核流程
 *
 * @author zyf
 */
@Component
public class PaymentPayStartProcessHandler implements ProcessStartHandler {

    @Autowired
    private BusPaymentService paymentService;
    @Autowired
    private BusOrderService orderService;

    /**
     * 初始化流程参数
     *
     * @param processInstanceRequest
     * @return
     */
    @Override
    public String init(ProcessInstanceRequest processInstanceRequest) {
        Long companyId = OpenHelper.getCompanyId();
        processInstanceRequest.setProcessKey("payment_pay_" + companyId);
        return processInstanceRequest.getBusinessKey();
    }

    /**
     * 设置全局流程变量
     *
     * @param processInstanceRequest
     * @param definition
     * @return
     */
    @Override
    public Map<String, Object> initVariables(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition) {
        Map<String, Object> map = new HashMap<>(5);
        BusPayment payment = paymentService.getById(processInstanceRequest.getBusinessKey());
        if(!payment.getPaymentState().equals(0)){
            ApiAssert.failure("请勿重复提交");
        }
        Long companyId = payment.getCompanyId();
        String orderId = payment.getOrderId();
        BigDecimal amount = payment.getAmount();
        if (FlymeUtils.isNotEmpty(orderId)) {
            BusOrder order = orderService.getById(orderId);
            if (FlymeUtils.isNotEmpty(order)) {
                map.put("orderNo", order.getOrderNo());
            }
        }
        String amountType = payment.getAmountType();
        Long deptId = payment.getDeptId();
        Long supplierId = payment.getSupplierId();
        String customId = payment.getCustomId();
        String paymentNo = payment.getPaymentNo();
        map.put("amountType", amountType);
        if (companyId.equals(1L)) {
            if (amountType.equals(AmountTypeEnum.Settle.getCode()) || amountType.equals(AmountTypeEnum.CbReturn.getCode())) {
                map.put("nqbz", 1);
            } else {
                map.put("nqbz", 0);
            }
        }
        //朗迪垫付款和开票税金金额为0的时候直接结束
        if (companyId.equals(4L)) {
            if (amountType.equals(AmountTypeEnum.SupplierBond.getCode()) || amountType.equals(AmountTypeEnum.Invoice.getCode())) {
                if (FlymeUtils.gtzero(amount)) {
                    map.put("cwsq", 0);
                } else {
                    map.put("cwsq", 2);
                }

            } else {
                map.put("cwsq", 1);
            }
        }
        //存放金额是否大于0变量
        if (FlymeUtils.gtzero(amount)) {
            map.put("amount_gtzero", 1);
        } else {
            map.put("amount_gtzero", 0);
        }
        if (FlymeUtils.isNotEmpty(deptId)) {
            map.put("deptId", deptId);
        }
        if (FlymeUtils.isNotEmpty(supplierId)) {
            map.put("supplierId", supplierId);
        }
        if (FlymeUtils.isNotEmpty(customId)) {
            map.put("customId", payment.getCustomId());
        }
        map.put("paymentNo", paymentNo);
        return map;
    }


    /**
     * 启动完成业务处理
     *
     * @param processInstanceRequest
     * @param definition
     * @param processInstance
     * @param tasks
     * @param variables
     */
    @Override
    public Boolean afterComplete(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition, ProcessInstance processInstance, List<Task> tasks, Map<String, Object> variables) {
        String businessKey = processInstanceRequest.getBusinessKey();
        Object cwsq = variables.get("cwsq");
        Integer state = CommonConstants.ENABLED;
        if (FlymeUtils.isNotEmpty(cwsq) && cwsq.equals(2)) {
            state = CommonConstants.INT_2;
        }
        //更新资金支付单为已提交并关联流程实例ID
        return paymentService.updatePaymentState(Long.parseLong(businessKey), processInstance.getProcessInstanceId(), state);
    }
}
